﻿#region File Description

// --------------------------------
// <copyright file="Animations.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
//     This document is shared by Microsoft Public License (MS-PL)
//     For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
// </copyright>
// <author>Vladislav Richter</author>

// <email>richtvl2@fit.cvut.cz</email>

// <date>9. 11. 2011 22:22:37</date>

// <summary>
//
// </summary>
// ------------------------------------------------------------------------------------------------------

#endregion File Description

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace ExternalClasses.Animations
{
    /// <summary>
    /// Stores data for all avaiable clips and their lists of bones they affect aka Takes
    /// </summary>
    public class Animations
    {
        public SkinningData skinningData;
        public AnimationLayers Layers;
        public AnimationTakes Takes;
        public Dictionary<String, AnimationTake> TakesDic;

        public Animations(ContentManager Content)
        {
            TakesDic = new Dictionary<string, AnimationTake>();
            Layers = Content.Load<AnimationLayers>("Animations\\Layers");
            Takes = Content.Load<AnimationTakes>("Animations\\Takes");
            skinningData = Content.Load<Model>(@"characters\default\character").Tag as SkinningData;
            Console.WriteLine("Accesible bones:");
            foreach (String s in skinningData.BoneIndices.Keys) Console.WriteLine(s);
            for (int i = 0; i < Takes.Takes.Count; i++)
            {
                Takes.Takes[i].Valid = false;
                for (int j = 0; j < Layers.Layers.Count; j++)
                {
                    if (Layers.Layers[j].Name == Takes.Takes[i].LayerName)
                    {
                        if (skinningData.AnimationClips.Keys.Contains(Takes.Takes[i].Clip))
                        {
                            Takes.Takes[i].Valid = true;
                            Takes.Takes[i].LayerIndex = j;
                            Takes.Takes[i].Layer = Layers.Layers[j];
                            Takes.Takes[i].ClipData = skinningData.AnimationClips[Takes.Takes[i].Clip];
                        }
                    }
                }
                if (Takes.Takes[i].Valid)
                {
                    TakesDic.Add(Takes.Takes[i].Name, Takes.Takes[i]);
                }
            }
            for (int i = 0; i < Layers.Layers.Count; i++)
            {
                Layers.Layers[i].ActiveBoneIndexes = new List<int>();
                foreach (String s in Layers.Layers[i].ActiveBones)
                {
                    if (skinningData.BoneIndices.Keys.Contains(s)) Layers.Layers[i].ActiveBoneIndexes.Add(skinningData.BoneIndices[s]);
                    else Console.WriteLine("ERROR: Bone of name: " + s + " does not exists!");
                }
            }
        }

        public AnimationTake GetTake(String name)
        {
            if (TakesDic.Keys.Contains(name))
            {
                return TakesDic[name];
            }
            Console.WriteLine("ERROR: Take of name: " + name + " does not exists!");
            return null;
        }
    }
}